import java.util.concurrent.CountDownLatch;

public class Test5 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch downLatch = new CountDownLatch(10);//参数表示拆分成的任务数
        for (int i = 0; i < 10; i++) {
            int id = i;
            Thread t = new Thread(()->{
                System.out.println(id+"线程开始执行");
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(id+"任务执行完毕");
                downLatch.countDown();//执行一次，相当于完成一个任务
            });
            t.start();
        }
        downLatch.await();//等待10个任务完成，才执行后续的逻辑
        System.out.println("10个任务执行完毕！");
    }
}
